/* MIT License
 *
 * Copyright (c) 2012 Reuben Smith, Miami University
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.droidlike.vsig;


import java.util.ArrayList;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;


/**
 * InfoTableModel.
 * 
 * @author smithre5
 */
class InfoTableModel 
    extends AbstractTableModel
{
    public InfoTableModel(JTable table)
    {
        this.table = table;
        info = new ArrayList<InfoTableEntry>();
    }
    
    
    public void addItem(String field, Object value)
    {
        info.add(new InfoTableEntry(field, value));
        fireTableRowsInserted(info.size() - 1, info.size() - 1);
    }
    
    
    public void clear()
    {
        int lastRow = info.size() == 0 ? 0 : info.size() - 1;
        info.clear();
        fireTableRowsDeleted(0, lastRow);
    }
    
    
    public int getColumnCount()
    {
        return COLUMN_NAMES.length;
    }
    
    
    @Override
    public String getColumnName(int col)
    {
        if (!Util.inRange(col, 0, COLUMN_NAMES.length)) {
            return null;
        }
        
        return COLUMN_NAMES[col];
    }
    
    
    public int getRowCount()
    {
        return info.size();
    }


    public Object getValueAt(int row, int col)
    {
        if (!Util.inRange(row, 0, info.size() - 1)) {
            return null;
        }
        
        if (col == 0) {
            return info.get(row).field;
        }
        else if (col == 1) {
            return info.get(row).value;
        }
        
        return null;
    }
    
    
    public void setInfo(Object[][] data)
    {
        if (data == null) {
            return;
        }
        
        clear();
        
        for (int i = 0; i < data.length; ++i) {
            info.add(new InfoTableEntry((String)data[i][0], data[i][1]));
        }
        
        fireTableRowsInserted(0, data.length - 1);
    }
    
    
    public void setValueAt(Object value, int row, int col)
    {
        if (!Util.inRange(row, 0, getRowCount() - 1) || !Util.inRange(col, 1, getColumnCount() - 1)) {
            return;
        }
        
        info.get(row).value = value;
        fireTableCellUpdated(row, col);
    }
    
    
    private class InfoTableEntry
    {
        public InfoTableEntry(String field, Object value)
        {
            this.field = field;
            this.value = value;
        }
        
        
        public String field;
        public Object value;
    }
    
    
    private static final String[] COLUMN_NAMES = new String[]{
        "Field",
        "Value"
    };
    
    private JTable table;
    private ArrayList<InfoTableEntry> info;
}